package com.be.beadmin.project.controller;

import com.be.beadmin.basicData.entity.User;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.utils.ResultModel;
import com.be.beadmin.project.dto.input.TeamInfoByTypeDto;
import com.be.beadmin.project.dto.input.TeamInfoDto;
import com.be.beadmin.project.dto.input.TeamInfoPageDto;
import com.be.beadmin.project.dto.output.TeamInfoByTypeEx;
import com.be.beadmin.project.dto.output.UserInfoSimpEx;
import com.be.beadmin.project.service.TeamInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
* @author zjm
* @date 2023-02-02
**/
@Api(tags = {"项目管理-团队成员模块"})
@RestController
public class TeamInfoController {

    @Autowired
    private  TeamInfoService teamInfoService;

    @ApiOperation("获取团队成员分页信息")
    @PostMapping(value = "/project/getTeamInfoByPage")
    public ResultModel getTeamInfoByPage(@RequestBody PageEntity pageEntity)  {
        return ResultModel.success(teamInfoService.getTeamInfoByPage(pageEntity)) ;
    }

    @ApiOperation("获取指定id的团队成员信息")
    @PostMapping(value = "/project/getTeamInfoByIdAndDelete")
    public ResultModel getTeamInfoByIdAndDelete(@RequestParam String teamInfoId)  {
        return ResultModel.success(teamInfoService.getTeamByIdAndDelete(teamInfoId)) ;
    }

    @ApiOperation("获取项目团队成员的所有角色类型")
    @PostMapping("/project/getRoleType")
    public ResultModel getRoleType(){
        List<String> roleTypeList = teamInfoService.getRoleType();
        return ResultModel.success(roleTypeList);
    }


    @ApiOperation("分页获得团队所有团队成员--按照角色分") // 分页查询
    @PostMapping("/project/getTeamInfosByPageAndRoleType")
    public ResultModel getTeamInfosByPageAndRoleType(@RequestBody TeamInfoPageDto teamInfoPageDto){
        return ResultModel.success(teamInfoService.getTeamInfosByPageAndRoleType(teamInfoPageDto));
    }

    @ApiOperation("新——获得团队所有团队成员--按照角色分") // 查询
    @PostMapping("/project/getAllTeamInfosByRoleType")
    public ResultModel getAllTeamInfosByRoleType(@RequestParam String teamId){
        return ResultModel.success(teamInfoService.getAllTeamInfosByRoleType(teamId));
    }

    @ApiOperation("新——获得团队某个角色的所有团队成员") // 单条记录查询
    @PostMapping("/project/getTeamInfosByRoleType")
    public ResultModel getTeamInfosByRoleType(@RequestParam String teamId, @RequestParam String roleType){
        return ResultModel.success(teamInfoService.getTeamInfosByRoleType(teamId,roleType));
    }

    @ApiOperation("新——为一种角色添加多个团队成员")
    @PostMapping("/project/addTeamInfoByRoleType") // 增加一条
    public ResultModel addTeamInfoByRoleType(@RequestBody TeamInfoByTypeDto teamInfoByTypeDto){
        teamInfoService.addTeamInfoByRoleType(teamInfoByTypeDto);
        return ResultModel.success(ResultModel.ADD);
    }

    @ApiOperation("新——为一种角色修改团队成员") // 修改单条数据
    @PostMapping("/project/updateTeamInfosByRoleType")
    public ResultModel updateTeamInfosByRoleType(@RequestBody TeamInfoByTypeDto teamInfoByTypeDto){
        teamInfoService.updateTeamInfosByRoleType(teamInfoByTypeDto);
        return ResultModel.success(ResultModel.UPDATE);
    }

    @ApiOperation("新——删除某一种角色团队成员") // 删除单条数据
    @PostMapping("/project/delTeamInfosByRoleType")
    public ResultModel delTeamInfosByRoleType(@RequestParam String teamId, @RequestParam String roleType){
        teamInfoService.delTeamInfosByRoleType(teamId,roleType);
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("新——删除某一个团队成员") // 删除单条数据
    @PostMapping("/project/delOneTeamMember")
    public ResultModel delOneTeamMember(@RequestParam(value= "teamId") @ApiParam(value = "团队Id",required = true) String teamId,
                                        @RequestParam(value = "userId") @ApiParam(value = "用户Id", required = true)String userId,
                                        @RequestParam(value = "roleId") @ApiParam(value = "角色Id",required = true)String roleId){
        teamInfoService.delOneTeamMember(teamId,userId,roleId);
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("新——批量删除某些角色团队成员") // 删除多条数据
    @PostMapping("/project/delBatchTeamInfosByRoleType")
    public ResultModel delBatchTeamInfosByRoleType(@RequestParam String teamId, @RequestParam List<String> roleTypes){
        teamInfoService.delBatchTeamInfosByRoleType(teamId,roleTypes);
        return ResultModel.success(ResultModel.DELETE);
    }



//    @PostMapping(value = "/project/getTeamInfoByTeamAndPage")
//    public ResultModel getTeamInfoByTeamAndPage(@RequestBody PageEntity pageEntity)  {
//        FilderExpressEntity filderExpress = new FilderExpressEntity();
//        filderExpress.setField("team_id");
//        filderExpress.setOperator("=");
//        filderExpress.setValue("");
//
//        pageEntity.setFilters();
//        return ResultModel.success(teamInfoService.getTeamInfoByPage(pageEntity)) ;
//    }

    @ApiOperation("添加一个团队成员")
    @PostMapping("/project/addTeamInfo")
    public ResultModel addTeamInfo(@RequestBody TeamInfoDto teamInfoDto){
        int result = teamInfoService.addTeamInfo(teamInfoDto);
        if (result == -1 ){
            return ResultModel.error(-1,"["+ teamInfoDto.getUserName()+"]团队成员已存在，请勿重复添加");
        }else if (result == -2){
            return ResultModel.error(-2,"["+ teamInfoDto.getCategory()+"]角色类型不存在，请检查");
        }else if(result == -3){
            return ResultModel.error(-3,"["+ teamInfoDto.getCategory()+"]各个经理只能有一个，请检查");
        }
        return ResultModel.success("["+ teamInfoDto.getUserName()+"]团队成员添加成功");
//        return ResultModel.success(ResultModel.ADD);
    }

//    @PostMapping("/project/addTeamInfos")
//    public ResultModel addTeamInfos(@RequestBody List<TeamInfoDto> teamInfoDtos){
//        teamInfoService.addTeamInfos(teamInfoDtos);
//        return ResultModel.success(ResultModel.ADD);
//    }
    @ApiOperation("删除一个团队成员")
    @PostMapping("/project/delTeamInfo")
    public ResultModel delTeamInfo(@RequestBody TeamInfoDto teamInfoDto){
        teamInfoService.delTeamInfo(teamInfoDto.getId());
        return ResultModel.success(ResultModel.DELETE);
    }

    @ApiOperation("修改一个团队成员的信息")
    @PostMapping("/project/updateTeamInfo")
    public ResultModel updateTeamInfo(@RequestBody TeamInfoDto teamInfoDto){
        int result = teamInfoService.updateTeamInfo(teamInfoDto);
//        return ResultModel.success(ResultModel.UPDATE);
        if (result == -1 ){
            return ResultModel.error(-1,"["+ teamInfoDto.getUserName()+"]团队id或用户id或用户名不可修改");
        }else if (result == -2){
            return ResultModel.error(-2,"["+ teamInfoDto.getCategory()+"]角色类型不存在，请检查");
        }else if(result == -3){
            return ResultModel.error(-3,"["+ teamInfoDto.getCategory()+"]各个经理只能有一个，请检查");
        }
        return ResultModel.success("["+ teamInfoDto.getUserName()+"]团队成员信息修改成功");
    }
    @ApiOperation("获取所有用户的简易信息")
    @PostMapping("/project/getUserInfoSimp")
    public ResultModel getUserInfoSimp(){
        List<UserInfoSimpEx> userInfoSimpDtos = teamInfoService.getUserInfoSimp();
        return ResultModel.success(userInfoSimpDtos);
    }

}